<?php

/**
 * CelebrioTV default
 * 
 * At this time streams from czech television are captured as iframes
 * in views. In future there should be API for that
 *
 * @copyright  Copyright (c) 2011 Celebrio Software
 * @package    CelebrioTV
 */

namespace ApplicationsModule\TVModule;

use WebtopModule\BasePresenter;

class DefaultPresenter extends BasePresenter {

    public function startup() {
        parent::startup();
        header("Content-type:text/html; charset=utf-8"); // TODO: poresit
        $this->getTemplate()->title = _("Television");
        $this->getTemplate()->appName = "television";
        if ($this->isAjax()) {
            $this->invalidateControl("celebriotvappSnippet");
        }
    }

    public function renderDefault() {
        
    }

    private function getStreamURL($web_url) {
        $stream = "";
        $html = @\file_get_contents($web_url);
        if (empty($html))
            return $stream;
        $embed_pos = \strpos($html, "<embed") + 6;
        if ($embed_pos == 6)
            return $stream;
        $data_pos = \strpos($html, 'data="', $embed_pos) + 6;
        $data_end_pos = \strpos($html, '"', $data_pos);
        $stream = \substr($html, $data_pos, $data_end_pos - $data_pos);
        unset($html);
        return $stream;
    }

    public function renderCT24() {
        //$this->template->ct24stream = $this->getStreamURL("http://www.ceskatelevize.cz/ivysilani-jako-driv/ct24-zive/?streamtype=wm2");
        /* $this->template->ct24stream = "";
          $html = @\file_get_contents("http://www.ct24.cz/vysilani");
          $divIframePositionStart = \strpos($html, '<div id="iFramePositionContainer"');
          $divIframePositionEnd = \strpos($html, "</div>", $divIframePositionStart);
          $divIframeLenght = $divIframePositionEnd - $divIframePositionStart;

          $divIframeString = \substr($html, $divIframePositionStart, $divIframeLenght);

          $srcPositionStart = \strpos($divIframeString, 'src="')+5;
          if($srcPositionStart == 5) return;
          $srcPositionEnd = \strpos($divIframeString, '"', $srcPositionStart);
          $srcStringLength = $srcPositionEnd - $srcPositionStart;
          $srcString = substr($divIframeString, $srcPositionStart, $srcStringLength);
          $srcString = "http://www.ct24.cz" . $srcString;
          unset($html);
          $this->template->ct24stream = $srcString; */
    }

    /* public function renderCT1() {
      $this->template->ct1stream = "";
      $html = @\file_get_contents("http://www.ceskatelevize.cz/ct1/zive/");
      if (empty($html)) return;
      $id_pos = \strpos($html, 'id="CTPlayer-CT1"')+17;
      if($id_pos == 17) {
      $next_program_start = \strpos($html, 'Živé vysílání bude pokračovat');
      $next_program_stop = \strpos($html, '</p>', $next_program_start);
      $this->template->next_program = \substr($html, $next_program_start, $next_program_stop-$next_program_start);
      return;
      }
      $web2_url_start = \strpos($html, '<a href="/ivysilani', $id_pos)+9;
      $web2_url_end = \strpos($html, '"', $web2_url_start);
      $web2_url = \substr($html, $web2_url_start, $web2_url_end-$web2_url_start);
      $this->template->test = $web2_url;
      $web3_url = \str_replace("ivysilani", "ivysilani-jako-driv", $web2_url);
      $this->template->ct1stream = "http://www.ceskatelevize.cz".$web3_url."?streamtype=wm2";
      unset($html);
      } */

    public function renderCT1() {
        /* $this->template->ct1stream = "";
          $html = @\file_get_contents("http://www.ceskatelevize.cz/ct1/zive"); */
    }

    public function renderCT2() {
        /* $this->template->ct2stream = "";
          $html = @\file_get_contents("http://www.ceskatelevize.cz/ct2/zive/");
          if (empty($html)) return;
          $id_pos = \strpos($html, 'id="CTPlayer-CT2"')+17; // TODO: overit
          if($id_pos == 17) {
          $next_program_start = \strpos($html, 'Živé vysílání bude pokračovat');
          $next_program_stop = \strpos($html, '</p>', $next_program_start);
          $this->template->next_program = \substr($html, $next_program_start, $next_program_stop-$next_program_start);
          return;
          }
          $web2_url_start = \strpos($html, '<a href="/ivysilani', $id_pos)+9;
          $web2_url_end = \strpos($html, '"', $web2_url_start);
          $web2_url = \substr($html, $web2_url_start, $web2_url_end-$web2_url_start);
          $web3_url = \str_replace("ivysilani", "ivysilani-jako-driv", $web2_url);
          $this->template->ct2stream = $this->getStreamURL("http://www.ceskatelevize.cz".$web3_url."?streamtype=wm2");
          unset($html); */
    }

    /* public function renderCT4() {
      $this->template->ct4stream = "";
      $html = @\file_get_contents("http://www.ct24.cz/sport/");
      if (empty($html)) return;
      $web2_url_start = \strpos($html, 'resizeButton"><a href="http://www.ceskatelevize.cz/ivysilani/')+23;
      if($web2_url_start == 23) {
      $next_program_start = \strpos($html, 'Právě teď běží na ČT4 pořad, který nemůžeme vysílat po internetu.');
      $next_program_stop = \strpos($html, '</span>', $next_program_start);
      $this->template->next_program = \substr($html, $next_program_start, $next_program_stop-$next_program_start);
      return;
      }
      $web2_url_end = \strpos($html, '"', $web2_url_start);
      $web2_url = \substr($html, $web2_url_start, $web2_url_end-$web2_url_start);
      $web3_url = \str_replace("ivysilani", "ivysilani-jako-driv", $web2_url);
      $this->template->ct4stream = $this->getStreamURL($web3_url."?streamtype=wm2");
      unset($html);
      } */

    public function renderCT4() {
        //$this->template->ct4stream = "";
        //$html = @\file_get_contents("http://www.ct24.cz/sport");
        //if (empty($html)) return;
//        $objectPositionStart = strpos($html, '<object id="CTPlayer-1')+21;
//        if($objectPositionStart == 21) {
//            //TODO set $nextProgram etc -- Is it necesary?
//        }
//        $objectPositionEnd = \strpos($html, '</object>');
//        
//        $paramPossitionStart = strpos($html, 'name="flashvars"'); //finds parameter flashvars in html
//        
//        if(empty($paramPossitionStart)) return;
        //$urlPositionStart = \strpos($html, 'http://', $paramPossitionStart);
        //$urlPositionEnd = \strpos($html, "&", $urlPositionStart);
        //$urlLength = $urlPositionEnd - $urlPositionStart;
        //$urlString = \substr($html, $urlPositionStart, $urlLength);
        //$this->template->ct4stream = $urlString;
        //$this->template->test = "ahoj";
    }

    public function renderCnn() {
        
    }

    public function renderBloombergTV() {
        $this->template->streamBloombergTV = "http://www.bloomberg.com/streams/video/LiveBTV200.asxx";
    }

    public function renderCBNNews() {
        $this->template->flashvars = null;
        $this->template->source = null;
        $html = @\file_get_contents("http://freetubetv.net/index.php?view=1Y2JuZXdz");
        $positionOfEmbed = \strpos($html, "<embed");
        $this->template->flashvars = $this->getFlashvars($html, $positionOfEmbed);
        $this->template->source = $this->getSource($html, $positionOfEmbed);
    }

    public function renderCSPAN1() {
        $this->template->flashvars = null;
        $this->template->source = null;
        $html = @\file_get_contents("http://freetubetv.net/index.php?view=1Y3NwYW4x");
        $positionOfEmbed = \strpos($html, "<embed");
//        \Nette\Debug::dump($this->getFlashvars($html, $positionOfEmbed));
//        \Nette\Debug::dump($this->getSource($html, $positionOfEmbed));
//        throw new \Exception();
        $this->template->flashvars = $this->getFlashvars($html, $positionOfEmbed);
        $this->template->source = $this->getSource($html, $positionOfEmbed);
    }

    public function renderCSPAN2() {
        $this->template->flashvars = null;
        $this->template->source = null;
        $html = @\file_get_contents("http://freetubetv.net/index.php?view=1Y3NwYW4y");
        $positionOfEmbed = \strpos($html, "<embed");
        $this->template->flashvars = $this->getFlashvars($html, $positionOfEmbed);
        $this->template->source = $this->getSource($html, $positionOfEmbed);
    }

    public function renderOckoTV() {
        $this->template->flashvars = null;
        $this->template->source = null;
        $html = @\file_get_contents("http://freetubetv.net/index.php?view=6b2Nrbw");
        $positionOfEmbed = \strpos($html, "<embed");
        $this->template->flashvars = $this->getFlashvars($html, $positionOfEmbed);
        $this->template->source = $this->getSource($html, $positionOfEmbed);
    }

    public function renderDeluxeMusic() {
        $this->template->isActive = false;
        $html = @\file_get_contents("http://freetubetv.net/index.php?view=6ZGVsdXhl");
        $positionOfIframe = \strpos($html, "<iframe");
        if ($positionOfIframe != 0) {
            $this->template->isActive = true;
        }
    }

    public function renderNasaTV() {
        /* $this->template->flashvars = null;
          $this->template->source = null;
          $html = @\file_get_contents("http://freetubetv.net/index.php?view=3bmFzYQ");
          $positionOfIframe = \strpos($html, "<embed");
          $this->template->flashvars = $this->getFlashvars($html, $positionOfEmbed);
          $this->template->source = $this->getSource($html, $positionOfEmbed); */
    }

    private function getFlashvars($html, $positionOfEmbed) {
        $flashvars = null;
        $fvPositionStart = \strpos($html, 'flashvars="', $positionOfEmbed) + 11;
        if ($fvPositionStart == 11)
            return $flashvars;
        $fvPositionEnd = \strpos($html, '"', $fvPositionStart);
        $flashvarsLength = $fvPositionEnd - $fvPositionStart;
        $flashvars = \substr($html, $fvPositionStart, $flashvarsLength);
        return $flashvars;
    }

    private function getSource($html, $positionOfEmbed) {
        $source = null;
        $sourcePosStart = \strpos($html, 'src="', $positionOfEmbed) + 5;
        if ($sourcePosStart == 5)
            return $source;
        $sourcePosEnd = \strpos($html, '"', $sourcePosStart);
        $sourceLength = $sourcePosEnd - $sourcePosStart;
        $source = \substr($html, $sourcePosStart, $sourceLength);
        return $source;
    }

}